Page({
    data: {
      addresses: []
    },
  
    onLoad(options) {
      const userId = wx.getStorageSync('userId'); // 获取本地存储的用户ID
  
      if (!userId) {
        wx.showToast({
          title: '用户未登录',
          icon: 'none'
        });
        return;
      }
  
      // 调用后端接口获取用户地址数据
      wx.request({
        url: `http://localhost:3000/getAddresses?userId=${userId}`,
        method: 'GET',
        success: (res) => {
          if (res.data.error) {
            wx.showToast({
              title: res.data.error,
              icon: 'none'
            });
          } else {
            this.setData({
              addresses: res.data
            });
          }
        },
        fail: () => {
          wx.showToast({
            title: '请求失败，请检查网络',
            icon: 'none'
          });
        }
      });
    },
  
    // 跳转到修改地址页面并传递选中的地址信息
    editAddress(event) {
      const address = event.currentTarget.dataset.address; // 获取点击的地址信息
      wx.navigateTo({
        url: `/pages/address/editaddress/editaddress?address=${JSON.stringify(address)}`, // 跳转并传递地址信息
      });
    },
  
    openAddAddress: function() {
      // 跳转到新增地址页面
      wx.navigateTo({
        url: '/pages/address/addaddress/addaddress'
      });
    }
  });
  